package com.shanzmoo.base.bean;


import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.base.constant.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


/**
 * @Description Http接口返回类
 * @Author       guizhao
 * @Date         2019/11/21 10:36
 **/
@Data
@ApiModel("返回响应")
public class SHttpResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(name = "errno", value = "成功 0； 失败-1", example = "0")
    private int errno;

    @ApiModelProperty(name = "errmsg", value = "返回描述", example = "成功")
    private String errmsg;

    @ApiModelProperty(name = "data", value = "结果集")
    private T data;

    private SHttpResponse() {}

    private SHttpResponse(int errno, String errmsg, T data) {
        this.errno = errno;
        this.errmsg = errmsg;
        this.data = data;
    }

    public static <T> SHttpResponse<T> ok() {
        return new SHttpResponse<T>(Constant.SUCCESS_CODE,"成功",null);
    }

    public static <T> SHttpResponse<T> ok(T data) {
        return new SHttpResponse<T>(Constant.SUCCESS_CODE,"成功",data);
    }

    public static <T> SHttpResponse<T> fail() {
        return new SHttpResponse<T>(502, "内部系统错误",null);
    }

    public static <T> SHttpResponse<T> fail(String message) {
        return new SHttpResponse<T>(Constant.ERRCODE_LOGIC, "内部系统错误:" + message,null);
    }

    public static <T> SHttpResponse<T> fail(ErrorCode errorCode) {
        return new SHttpResponse<T>(errorCode.code(), errorCode.desc(), null);
    }

    public static <T> SHttpResponse<T> fail(int errno, String errmsg) {
        return new SHttpResponse<T>(errno, errmsg,null);
    }
    public static <T> SHttpResponse<T> fail(int errno, String errmsg,T data) {
        return new SHttpResponse<T>(errno, errmsg,data);
    }

    public static <T> SHttpResponse<T> transform(SResponse<T> re) {
        return new SHttpResponse<>(re.getErrno(),re.getErrmsg(),(T)re.getData());
    }


    public boolean isOk() {
        return errno == Constant.SUCCESS_CODE;
    }
}
